package com.demo.studentidcard.dao.impl;

import com.demo.studentidcard.dao.AccountDao;
import com.demo.studentidcard.entity.Account;
import com.demo.studentidcard.entity.RecordsOfConsumption;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 8
 * @date 2021/9/24 10:41
 */
@Slf4j
@Repository
public class AccountDaoImpl implements AccountDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public AccountDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public Account getAccount(String id) {
        String sql = "select * from card_info where id=?;";
        Object[] object = new Object[]{id};
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Account.class), object);
        } catch (Exception e){
            e.printStackTrace();
            return new Account("0", "0", "0");
        }
    }

    @Override
    public boolean addAccount(Account account) {
        Object[] objects = new Object[]{
                account.getId(),
                account.getName(),
                account.getPassword()
        };
        String sql = "insert into card_info (id, name , password) values (?, ?, ?);";
        try {
            jdbcTemplate.update(sql, objects);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean recharge(String id, int money) {
        String sql = "update card_info set balance = balance + ? where id = ?";
        Object[] object = new Object[]{money, id};
        try {
            jdbcTemplate.update(sql, object);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public int getBalance(String id) {
        Account account = getAccount(id);
        return account.getBalance();
    }

    @Override
    public boolean setIsLost(String id) {
        String sql = "update card_info set isLost=1 where id = ?";
        Object[] object = new Object[]{id};
        try {
            jdbcTemplate.update(sql, object);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean setIsLostNone(String id) {
        String sql = "update card_info set isLost=0 where id = ?";
        Object[] object = new Object[]{id};
        try {
            jdbcTemplate.update(sql, object);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<RecordsOfConsumption> getRecordsOfConsumption(String id) {
        String sql = "select * from record_of_consumption where id = ?;";
        Object[] objects = new Object[]{id};
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RecordsOfConsumption.class), objects);
    }
}
